﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace Print
{
    class InitFile
    {
        MsSqlHelper mssql = new MsSqlHelper();

        #region 去掉表格空格
        public DataTable DataView(DataTable dt)
        {
            try
            {
                string items = "";
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    items += "'" + dt.Columns[i].ToString().Trim() + "',";
                }
                items = items.LastIndexOf(",") == items.Length - 1 ? items.Remove(items.Length - 1, 1) : items;
                string sql = "select rtrim(chnname) chname,rtrim(fdname) fdname from [fldlist] where fdname in(" + items + ")";

                DataTable dtx = mssql.ExecuteDt(sql);
                Dictionary<string, string> param = new Dictionary<string, string>();
                Dictionary<string, bool> paramvisible = new Dictionary<string, bool>();
                foreach (DataRow dr in dtx.Rows)
                {
                    param.Add(dr["fdname"].ToString().ToLower().Trim(), dr["chname"].ToString());
                }

                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    if (param.ContainsKey(dt.Columns[i].ToString().ToLower().Trim()) && !dt.Columns.Contains(param[dt.Columns[i].ToString().ToLower().Trim()]))
                    {
                            dt.Columns[i].ColumnName = param[dt.Columns[i].ToString().ToLower().Trim()];
                    }
                }
                for (int k = 0; k < dt.Rows.Count; k++)
                {
                    for (int b = 0; b < dt.Columns.Count; b++)
                    {
                        if (dt.Rows[k][b].GetType() == typeof(String))
                        {
                            string temp = dt.Rows[k][b].ToString().Trim();
                            dt.Rows[k][b] = temp;
                            //WriteLog(temp, true);
                        }
                    }
                }
                
            }
            catch (Exception ex)
            {
                WriteLog(ex.ToString(), true);
            }
            return dt;
        }
        #endregion

        #region 日志
        public void WriteLog(string msg, bool isdebug)
        {
            string text = AppDomain.CurrentDomain.BaseDirectory;
            text = Path.Combine(text, "log");
            if (!Directory.Exists(text))
            {
                Directory.CreateDirectory(text);
            }
            if (isdebug)
            {
                text = Path.Combine(text, "debug");
                if (!Directory.Exists(text))
                {
                    Directory.CreateDirectory(text);
                }
            }
            text = Path.Combine(text, DateTime.Now.ToString("yyyyMMdd") + ".txt");
            if (!File.Exists(text))
            {
                File.CreateText(text).Close();
            }
            StreamWriter streamWriter = new StreamWriter(text, true, Encoding.UTF8);
            if (isdebug)
            {
                streamWriter.WriteLine("--------------------------------------------------------------------------------------------------------------------------------");
            }
            streamWriter.WriteLine("{0}  {1}", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), msg);
            if (isdebug)
            {
                streamWriter.WriteLine("--------------------------------------------------------------------------------------------------------------------------------");
            }
            streamWriter.Flush();
            streamWriter.Close();
        }

        public void ClearLog()
        {
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log");
            if (Directory.Exists(path))
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(path);
                FileInfo[] files = directoryInfo.GetFiles("*.*", SearchOption.AllDirectories);
                FileInfo[] array = files;
                for (int i = 0; i < array.Length; i++)
                {
                    FileInfo fileInfo = array[i];
                    if (DateTime.Now.Subtract(fileInfo.CreationTime).Days > 30)
                    {
                        File.Delete(fileInfo.FullName);
                    }
                }
            }
        }
        #endregion       
    }
}
